// /home/lester/HDPU/kinematix/kinematix/kPythonInterpreter.cs created with MonoDevelop
// User: lester at 22:26 17.06.2007
//

using System;
using IronPython.Hosting;
using IronPython.Runtime;


namespace kinematix
{
	
	
	public class kPythonInterpreter
	{
		
		public kApplication app;
		
		PythonEngine en;
		string cached_code;
		
		public kPythonInterpreter(kApplication _app)
		{
			app = _app;
			en = new PythonEngine (); // create a python engine. Yes it's so easy!
			en.AddToPath(app.CurrentDir+"/python"); // point to him our scripts dir
			cached_code = ""; 
			
		}
		
		public void SetupGlobals(kApplication app, kRenderer r)
		{
			// point to the engine our interfaces to the render and to the python interpreter
			en.Globals.Add("__interp__", this);			
			en.Globals.Add("__render__", r);
		}
		
		public void Quit()
		{
			en.Shutdown ();
		}
		
		public void Run(string code)
		{
			cached_code = code;
			Reload();
		}
		
		public void Reload()
		{
			// execute the code if it's not empty
			if (cached_code!= ""){
				en.Execute (cached_code);
			}
		}
		
		/*
		 * Usefult function for getting time in the script
		 */
		public uint GetTime()
		{
			return 	app.Renderer.Device.Timer.Time;
		}
	}
}
